require(['./config'], () => {
  require(['template', 'header', 'footer', 'cookie'], (template) => {
    class Cart {
      constructor() {
        this.init()
        this.jump()
      }
      init() {
        let cartList = localStorage.getItem('cart')
        this.cartList = JSON.parse(cartList)
        cartList = JSON.parse(cartList)
        if (cartList.length !=0) {
          
          $('#cartbox').html(template('cartTemplate', { cartList: this.cartList }))

          $('.all-checked').prop('checked', this.cartList.every(cart => {
            return cart.check
          }))

          // 有数据的话就要默认计算一次总价
          this.calAllmoney()
          this.calAllnum()
          this.calcTotalMoney()
          this.calTotalNum()
          // 给所有单选绑事件
          this.checksChange()

          this.allCheck()

          this.delChoose()

          this.delEach()

          this.sub()

          this.add()

          this.change()

        } else {
          // 购物车没有数据
          console.log(1111)
          $('#shoptable').hide()
          $('.gotobuy').show()
          
        }
      }

      // 计算总价
      calcTotalMoney() {
        $('#totalMoney').html(this.cartList.reduce((money, cart) => {
          if (cart.check) money += cart.price * cart.num
          return money
        }, 0).toFixed(2))
      }

      calAllmoney () {
        $('.Allprice').html(this.cartList.reduce((money, cart) => {
          money += cart.price * cart.num
          return money
        }, 0).toFixed(2))
      }


      // 计算总数
      calTotalNum() {
        $('#totalNum').html(this.cartList.reduce((num, cart) => {
          if (cart.check) num += cart.num
          return num
        }, 0))
      }

      calAllnum () {
        $('.Allnum').html(this.cartList.reduce((num, cart) => {
          num += cart.num
          return num
        }, 0))
      }

      // 单选
      checksChange() {
        const _this = this
        // 给所有的单选按钮绑事件
        $('#cartbox tbody input:checkbox').on('change', function () {
          // 取到当前商品的id
          let id = $(this).parents('tr').attr('data-id')
          // 根据id修改当前商品的选中状态
          _this.cartList = _this.cartList.map(cart => {
            if (cart.id == id) cart.check = $(this).prop('checked')
            return cart
          })
          // 重新存localStorage
          localStorage.setItem('cart', JSON.stringify(_this.cartList))
          // 全选按钮根据数据是否全选来渲染状态
          $('.all-checked').prop('checked', _this.cartList.every(cart => {
            return cart.check
          }))
          // 计算总价
          _this.calcTotalMoney()
          _this.calTotalNum()
        })
      }

      // 全选
      allCheck() {
        const _this = this
        $('.all-checked').on('change', function () {
          _this.cartList = _this.cartList.map(cart => {
            cart.check = $(this).prop('checked')
            return cart
          })
          localStorage.setItem('cart', JSON.stringify(_this.cartList))

          $('.all1').prop('checked', _this.cartList.every(cart => {
            return cart.check
          }))

          $('.all2').prop('checked', _this.cartList.every(cart => {
            return cart.check
          }))

          $('#cartbox tbody input:checkbox').prop('checked', _this.cartList.every(cart => {
            return cart.check
          }))

          _this.calcTotalMoney()
          _this.calTotalNum()

          // _this.checksChange()

        })
      }

      // 删除选中
      delChoose() {
        const _this = this
        $('.del-choose').on('click', function () {
          _this.cartList = _this.cartList.filter(cart => {
            return cart.check == false
          })

          $('.ch').filter(function () {
            return $(this).prop('checked') == true
          }).parent('td').parent('tr').remove()
          // 全选状态
          $('.all-checked').prop('checked', _this.cartList.every(cart => {
            return cart.check
          }))

          localStorage.setItem('cart', JSON.stringify(_this.cartList))

          let cartList = localStorage.getItem('cart')
          cartList = JSON.parse(cartList)

          if (cartList.length === 0) {
            $('#shoptable').hide()
            console.log($('.gotobuy'))
            // $('.gotobuy').show()
            console.log('!')
          }

          _this.calAllmoney()
          _this.calAllnum()
          _this.calcTotalMoney()
          _this.calTotalNum()
        })
      }

      // 删除单个
      delEach() {
        const _this = this
        $('.delbtn').on('click', function () {
          let id = $(this).parents('tr').attr('data-id')
          _this.cartList = _this.cartList.filter(cart => {
            return cart.id != id
          })

          localStorage.setItem('cart', JSON.stringify(_this.cartList))

          $(this).parents('tr').remove()

          // 全选状态
          $('.all-checked').prop('checked', _this.cartList.every(cart => {
            return cart.check
          }))

          _this.calAllmoney()
          _this.calAllnum()
          
          if ($(this).parents('tr').children('td').children('.ch').prop('checked') == true) {
            _this.calcTotalMoney()
            _this.calTotalNum()
          }
        })
      }

      //减1btn
      sub () {
        const _this = this
        $('.subbtn').click(function () {
          
          let id = $(this).parents('tr').attr('data-id')

          $(this).next('input')[0].value <= 1 ? 1 : $(this).next('input')[0].value--

          _this.cartList = _this.cartList.map(cart => {
            if (cart.id == id) {
              cart.num = Number($(this).next('input')[0].value)
              $(this).parent('td').next('td').children('.eachsum').html((cart.num * cart.price).toFixed(2))
            }
            return cart
          })

          localStorage.setItem('cart', JSON.stringify(_this.cartList))

          _this.calAllmoney()
          _this.calAllnum()
          if ($(this).parents('tr').children('td').children('.ch').prop('checked') == true) {
            _this.calcTotalMoney()
            _this.calTotalNum()
          }
        
        })

      }

      // 加1btn
      add () {
        const _this = this
        $('.addbtn').click(function () {
          let id = $(this).parents('tr').attr('data-id')
          $(this).prev('input')[0].value++
          _this.cartList = _this.cartList.map(cart => {
            if (cart.id == id) {
              cart.num = Number($(this).prev('input')[0].value)
              $(this).parent('td').next('td').children('.eachsum').html((cart.num * cart.price).toFixed(2))
            }
            return cart
          })
          localStorage.setItem('cart', JSON.stringify(_this.cartList))

          _this.calAllmoney()
          _this.calAllnum()

          if ($(this).parents('tr').children('td').children('.ch').prop('checked') == true) {
            _this.calcTotalMoney()
            _this.calTotalNum()
          }
        })
      }


      // 用户修改
      change() {
        const _this = this
        $('.inum').blur(function () {
          let id = $(this).parents('tr').attr('data-id')

          _this.cartList = _this.cartList.map(cart => {
            if (cart.id == id) {
              cart.num = Number($(this)[0].value)
              $(this).parent('td').next('td').children('.eachsum').html((cart.num * cart.price).toFixed(2))
            }
            return cart
          })
          localStorage.setItem('cart', JSON.stringify(_this.cartList))

          _this.calAllmoney()
          _this.calAllnum()

          if ($(this).parents('tr').children('td').children('.ch').prop('checked') == true) {
            _this.calcTotalMoney()
            _this.calTotalNum()
          }

          // console.log(11111)

        })
      }

      // 跳转结算页面
      jump() {

        $('.jump').click(function() {
          console.log($.cookie('userInfo'))
          if ($.cookie('userInfo')) {
            console.log(1111)
            window.location.href = '/html/statement.html'
          } else {
            alert('请登录')
            window.location.href = '/html/login.html'
          }

        })
        
      }





    }
    new Cart()
  })
})